package com.google.code.avajhelpdesk.persistence;

import java.util.List;

import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.google.code.avajhelpdesk.entities.User;


@Repository
public class UserDAO extends HibernateDaoSupport implements IUserDAO {
	
	/* (non-Javadoc)
	 * @see ru.ifmo.support.persistence.IUserDAO#findUser(java.lang.String, java.lang.String)
	 */
	@Transactional
	public List findUser(String username, String password) {
		return getSession().createCriteria(User.class)
				.add(Restrictions.eq("username", username))
				.add(Restrictions.eq("password", password)).list();
	}
	
	/* (non-Javadoc)
	 * @see ru.ifmo.support.persistence.IUserDAO#findUser(java.lang.String)
	 */
	@Transactional
	public List findUser(String username) {
		return getSession().createCriteria(User.class)
				.add(Restrictions.eq("username", username)).list();
	}

	@SuppressWarnings("unchecked")
	public List<User> findUsers(int first, int count, String property,
			boolean ascending) {
		
		return getSession().createCriteria(User.class)
					.addOrder((ascending) ? Order.asc(property) : Order.desc(property))
					.setFirstResult(first)
					.setMaxResults(count)
					.list();
	}
}
